Imports System.Data
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Partial Class Management_Info_Generate
    Inherits WebPageCommon

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim pFunc As New PolyFunc.Secure

            ViewState.Add("reportmode", pFunc.DecodeBase64(Request.QueryString("Q")))
            pFunc = Nothing
            tdStatus.Visible = False

            Select Case ViewState("reportmode")
                Case "Commision"
                    lblTitle.Text = "Commision Journal"
                    trDtl1.Visible = False
                    trDtl2.Visible = False

                Case "Issue"
                    lblTitle.Text = "Policy Issues"
                    tdStatus.Visible = True

                    Dim l As New LookUp.FillLookUp
                    l.FillDropDown(Status_cmb, LookUp.FillLookUp.eLookUpType.other, "ISSUE_STATUS")
                    l = Nothing

                Case "PolicySubmit"
                    lblTitle.Text = "New Policies on Client Servicing"
            End Select
        End If
    End Sub

    Private Sub DoSearch()
        Dim cf As New CIMSfunc

        Select Case ViewState("reportmode")
            Case "Commision"
                Dim dao As New DAOMain.VW_RPT_COMMISION_JOURNAL_DAO
                Dim vo As New DAOMain.VW_RPT_COMMISION_JOURNAL_VO

                With vo
                    If Trim(Startdate_txt.Text) <> "" Then .COM_DT = cf.ParseSetDate(Startdate_txt.Text)
                    If Trim(Enddate_txt.Text) <> "" Then .COM_DT_END = cf.ParseSetDate(Enddate_txt.Text)
                End With

                Dim sds As New DataSet
                Dim dt As DataTable = dao.Search(vo, , True)

                If dt.Rows.Count > 0 Then
                    sds.Tables.Add(dt)
                    Call RenderReport(sds, "vw_rpt_commision_journal", "vw_rpt_commision_journal.rpt")
                Else
                    Dim pf As New PolyFunc.ASPFunc

                    pf.MessageAlert(Me, "No Records Found")
                    pf = Nothing
                End If

            Case "Issue"
                Dim dao As New DAOMain.VW_POLICY_ISSUE_DTL_SEARCH_DAO
                Dim vo As New DAOMain.VW_POLICY_ISSUE_DTL_SEARCH_VO

                With vo
                    If Trim(Startdate_txt.Text) <> "" Then .POLICY_ISSUE_DT = cf.ParseSetDate(Startdate_txt.Text)
                    If Trim(Enddate_txt.Text) <> "" Then .POLICY_ISSUE_DT_END = cf.ParseSetDate(Enddate_txt.Text)
                    .CLIENTNAME = "%" & Client_txt.Text & "%"
                    .POLICY_NO = "%" & Policynumber_txt.Text & "%"
                    .PROVIDER_DESC = "%" & Provider_txt.Text & "%"
                    .PRDT_DESC = "%" & Product_txt.Text & "%"
                    .STATUS = Status_cmb.SelectedItem.Text
                End With

                Dim sds As New DataSet
                Dim dt As DataTable = dao.Search(vo, , True)

                If dt.Rows.Count > 0 Then
                    sds.Tables.Add(dt)
                    Call RenderReport(sds, "vw_policy_issue_dtl_search", "vw_policy_issue_dtl_search.rpt")
                Else
                    Dim pf As New PolyFunc.ASPFunc

                    pf.MessageAlert(Me, "No Records Found")
                    pf = Nothing
                End If
                
            Case "PolicySubmit"
                Dim vo As New DAOMain.VW_RPT_POLICY_SUBMIT_DTL_VO
                Dim dao As New DAOMain.VW_RPT_POLICY_SUBMIT_DTL_DAO

                With vo
                    If Trim(Startdate_txt.Text) <> "" Then .POLICY_SUBMIT_DT = cf.ParseSetDate(Startdate_txt.Text)
                    If Trim(Enddate_txt.Text) <> "" Then .END_DT = cf.ParseSetDate(Enddate_txt.Text)
                    .NAME = "%" & Client_txt.Text & "%"
                    .POLICY_NO = "%" & Policynumber_txt.Text & "%"
                    .PROVIDER_DESC = "%" & Provider_txt.Text & "%"
                    .PRDT_DESC = "%" & Product_txt.Text & "%"

                End With

                Dim dt As DataTable = dao.Search(vo, , True)

                dao = Nothing
                vo = Nothing

                If dt.Rows.Count > 0 Then
                    Dim sds As New DataSet

                    sds.Tables.Add(dt)

                    Call RenderReport(sds, "vw_rpt_policy_submit_dtl", "vw_rpt_policy_submit_dtl.rpt")
                Else
                    Dim pf As New PolyFunc.ASPFunc

                    pf.MessageAlert(Me, "No Records Found")
                    pf = Nothing
                End If

                
        End Select

        cf = Nothing
    End Sub

    Private Sub RenderReport(ByVal sDS As DataSet, ByVal TableName As String, ByVal ReportName As String)
        Dim sConn As New Data.SqlClient.SqlConnection
        Dim sCom As New Data.SqlClient.SqlCommand
        Dim sDApt As New Data.SqlClient.SqlDataAdapter(sCom)

        Try

            sDS.Tables(0).TableName = TableName

            Dim objReport As New ReportDocument

            'MsgBox(Server.MapPath(Session("sqlUseReport").ToString))
            objReport.Load(Server.MapPath(ReportName))

            'objReport.PrintOptions.PaperOrientation = PaperOrientation.Landscape
            objReport.SetDataSource(sDS)

            Dim expReport As ExportOptions
            expReport = objReport.ExportOptions
            expReport.FormatOptions = New PdfRtfWordFormatOptions 'PDF format option
            expReport.ExportFormatType = ExportFormatType.PortableDocFormat 'Set the format type of document
            Dim req As ExportRequestContext = New ExportRequestContext
            req.ExportInfo = expReport

            'Sets the report to HTML stream
            Dim st As System.IO.Stream
            st = objReport.FormatEngine.ExportToStream(req)

            objReport = Nothing 'Dispose the report

            'Stream content
            Response.ClearContent()
            Response.ClearHeaders()
            Response.ContentType = "application/pdf"

            Dim b(st.Length) As Byte
            st.Read(b, 0, st.Length) 'Read Stream
            Response.BinaryWrite(b) 'Write Stream
            st.Close() 'Close Stream
            st = Nothing 'Dispose

            Response.End()

        Catch ex As Exception

        Finally
            sConn.Close()
        End Try
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Call DoSearch()
    End Sub
End Class
